package cn.hpclub.server.service;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.LocalizedConfig;
import com.jfinal.plugin.activerecord.Db;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PackageLocalizedConfigService {
    private static final Logger logger          = LoggerFactory.getLogger(PackageLocalizedConfigService.class);
    public static final PackageLocalizedConfigService service = new PackageLocalizedConfigService();

    public void deleteConfig(Integer[] ids) {

        deleteLocalizedConfig(LocalizedConfig.TYPE_PACKAGE_XHRW,ids);
        //删除业务授权的配置，该ID的配置，不在1003，就在1004。因为套餐的类型可变。暂时这样设计。如果编辑过，原来的配置未删除掉。可能存在两个都有的情况。
        LocalizedConfig.dao.deleteAuthConfig(Constant.Table.TABLE_BUSINESS_AUTH_CONFIG,Constant.GeneralMemberAuthConfigBusinessType.XHRW,ids);
        LocalizedConfig.dao.deleteAuthConfig(Constant.Table.TABLE_BUSINESS_AUTH_CONFIG,Constant.GeneralMemberAuthConfigBusinessType.YXHDXHRW,ids);

        //删除泛店员的配置，该ID的配置，不在1003，就在1004。因为套餐的类型可变。暂时这样设计。
        LocalizedConfig.dao.deleteAuthConfig(Constant.Table.TABLE_SPREAD_MEMBER_AUTH_CONFIG,Constant.SpreadMemberAuthConfigBusinessType.XHRW,ids);
        LocalizedConfig.dao.deleteAuthConfig(Constant.Table.TABLE_SPREAD_MEMBER_AUTH_CONFIG,Constant.SpreadMemberAuthConfigBusinessType.YXHDXHRW,ids);
    }


    /**
     * 更新属地化地市信息，新增，或删除等
     * @param pkgId
     * @param localized
     * @param isEdit
     * @param oldLocalized
     * @throws Exception
     */
    public void saveLocalizedConfig(int pkgId,int localized,boolean isEdit,int oldLocalized,Integer[] cityIds)  {

        //增加属地化地市信息
        LocalizedConfig.dao.localizedConfig(LocalizedConfig.TYPE_PACKAGE_XHRW, pkgId, localized, cityIds);
        if(isEdit){
            //套餐做编辑时，需要调整授权数据。编辑时，才需要去变更泛店员和店员的业务授权表

            //删除普通业务授权配置数据。pkgid不在1003，就在1004
            LocalizedConfig.dao.deleteAuthConfig(Constant.Table.TABLE_BUSINESS_AUTH_CONFIG,Constant.GeneralMemberAuthConfigBusinessType.XHRW,pkgId,oldLocalized,localized,cityIds);
            LocalizedConfig.dao.deleteAuthConfig(Constant.Table.TABLE_BUSINESS_AUTH_CONFIG,Constant.GeneralMemberAuthConfigBusinessType.YXHDXHRW,pkgId,oldLocalized,localized,cityIds);

            //删除泛店员配置数据。pkgid不在1003，就在1004
            LocalizedConfig.dao.deleteAuthConfig(Constant.Table.TABLE_SPREAD_MEMBER_AUTH_CONFIG,Constant.SpreadMemberAuthConfigBusinessType.XHRW,pkgId,oldLocalized,localized,cityIds);
            LocalizedConfig.dao.deleteAuthConfig(Constant.Table.TABLE_SPREAD_MEMBER_AUTH_CONFIG,Constant.SpreadMemberAuthConfigBusinessType.YXHDXHRW,pkgId,oldLocalized,localized,cityIds);
//            LocalizedConfig.dao.deleteAuthConfig(LocalizedConfig.TYPE_PACKAGE_XHRW,pkgId,oldLocalized,localized,cityIds);
        }
    }

    private void deleteLocalizedConfig(int business, Integer[] ids){
        StringBuffer sb = new StringBuffer();
        sb.append("delete from localized_config where business ="+business+" and business_id in (");
        if(ids.length > 0){
            for(int i = 0; i < ids.length; i++){
                // System.out.println("id:" + ids[i]);
                if(i == ids.length - 1){
                    sb.append("?");
                } else{
                    sb.append("?,");
                }
            }
            sb.append(")");
        }

        Db.update(sb.toString(), ids);
    }

}
